package com.hu.zhcc.shiro.controller.xsx;


import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hu.zhcc.shiro.entity.xsx.*;
import com.hu.zhcc.shiro.service.xsx.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xsx
 * @since 2021-06-22
 */
@RestController
@RequestMapping("/tMuban")
public class TMubanController {

    @Autowired
    TMubanService tMubanService;
    @Autowired
    TMubanYinshiService tMubanYinshiService;
    @Autowired
    TMubanYundongService tMubanYundongService;
    @Autowired
    TMubanJiankangService tMubanJiankangService;
//    @Autowired
//    TMealsService tMealsService;
//    @Autowired
//    TSportsItemLibraryService tSportsItemLibraryService;
//    @Autowired
//    THealthPlanService tHealthPlanService;

    @GetMapping("/all")
    private List<XsxTMuban> queryAll(){
        return tMubanService.queryAllMuban();
    }

    @GetMapping("/qy")
    private List<XsxTMuban> queryQy(){
        return tMubanService.queryQyMuban();
    }

    @PutMapping("/{num}/id")
    private int updateStatue(@PathVariable("num") Integer num, Integer id){
        XsxTMuban tMuban=new XsxTMuban();
        tMuban.setMbid(id);
        if(num==0){
            tMuban.setMbstatue("禁用");
            return tMubanService.updStatue(tMuban);
        }
        if(num==1){
            tMuban.setMbstatue("启用");
            return tMubanService.updStatue(tMuban);
        }
        return 0;
    }

    //添加
    @PostMapping("/{muban}/{ys}/{yd}/{jk}")
    public int addMuban(@PathVariable("muban")String muban,String ys,String yd,String jk){
        Gson gson=new Gson();
        XsxTMuban tMuban =gson.fromJson(muban, new TypeToken<XsxTMuban>() {}.getType());
        tMubanService.addMuban(tMuban);
        Integer mbid = tMuban.getMbid();
        XsxTMubanYinshi tMubanYinshi=new XsxTMubanYinshi();
        XsxTMubanYundong tMubanYundong=new XsxTMubanYundong();
        XsxTMubanJiankang tMubanJiankang=new XsxTMubanJiankang();
        tMubanJiankang.setMubanId(mbid);
        tMubanYundong.setMubanId(mbid);
        tMubanYinshi.setMubanId(mbid);
        Integer[] arr =gson.fromJson(ys, new TypeToken<Integer[]>() {}.getType());
        for(int num : arr){
            tMubanYinshi.setYinshiId(num);
            tMubanYinshiService.addOne(tMubanYinshi);
        }

        Integer[] arr2 =gson.fromJson(yd, new TypeToken<Integer[]>() {}.getType());
        for(int num : arr2){
            tMubanYundong.setYundongId(num);
            tMubanYundongService.addOne(tMubanYundong);
        }

        Integer[] arr3 =gson.fromJson(jk, new TypeToken<Integer[]>() {}.getType());
        for(int num : arr3){
            tMubanJiankang.setJiankangId(num);
            tMubanJiankangService.addOne(tMubanJiankang);
        }
        return 1;
    }

    @PostMapping("/{id}")
    private XsxTMuban queryById(@PathVariable("id") Integer id){
        return tMubanService.queryById(id);
    }


    //修改
    @PutMapping("/{muban}/{ys}/{yd}/{jk}")
    public int updateMuban(@PathVariable("muban")String muban,String ys,String yd,String jk){
        Gson gson=new Gson();
        XsxTMuban tMuban =gson.fromJson(muban, new TypeToken<XsxTMuban>() {}.getType());
        tMubanService.updateMuban(tMuban);
        XsxTMubanYinshi tMubanYinshi=new XsxTMubanYinshi();
        XsxTMubanYundong tMubanYundong=new XsxTMubanYundong();
        XsxTMubanJiankang tMubanJiankang=new XsxTMubanJiankang();
        tMubanJiankang.setMubanId(tMuban.getMbid());
        tMubanYundong.setMubanId(tMuban.getMbid());
        tMubanYinshi.setMubanId(tMuban.getMbid());
        tMubanYinshiService.deleteByMbid(tMuban.getMbid());
        Integer[] arr =gson.fromJson(ys, new TypeToken<Integer[]>() {}.getType());
        for(int num : arr){
            tMubanYinshi.setYinshiId(num);
            tMubanYinshiService.addOne(tMubanYinshi);
        }
        tMubanYundongService.deleteByMbid(tMuban.getMbid());
        Integer[] arr2 =gson.fromJson(yd, new TypeToken<Integer[]>() {}.getType());
        for(int num : arr2){
            tMubanYundong.setYundongId(num);
            tMubanYundongService.addOne(tMubanYundong);
        }
        tMubanJiankangService.deleteByMbid(tMuban.getMbid());
        Integer[] arr3 =gson.fromJson(jk, new TypeToken<Integer[]>() {}.getType());
        for(int num : arr3){
            tMubanJiankang.setJiankangId(num);
            tMubanJiankangService.addOne(tMubanJiankang);
        }
        return 1;
    }


}

